package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.AttendenceDTO;
import com.work.libtest2.entity.Attendence;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.mapper.AttendenceMapper;
import com.work.libtest2.mapper.EmployeeMapper;
import com.work.libtest2.service.AttendencecService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AttendenceServiceImpl
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 15:47
 * @Version 1.0
 */
@Service
public class AttendenceServiceImpl extends ServiceImpl<AttendenceMapper, Attendence> implements AttendencecService {

    @Resource
    private AttendenceMapper attendenceMapper;
    @Resource
    private EmployeeMapper employeeMapper;
//    签到的状态 0为迟到 1为正常签到 2为早退
    @Override
    public IPage<AttendenceDTO> getPage(Integer currentPage, Integer pageSize, AttendenceDTO attendenceDTO) {
        IPage page = new Page(currentPage,pageSize);
        attendenceMapper.selectPage(page,null);

        List<AttendenceDTO> list = new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            AttendenceDTO attendenceDTOTemp = new AttendenceDTO();
            Attendence attendence = (Attendence) page.getRecords().get(i);
            BeanUtils.copyProperties(attendence,attendenceDTOTemp);

//            根据员工id查询员工姓名
            Employee employee = employeeMapper.selectById(attendenceDTOTemp.getEmpId());
            attendenceDTOTemp.setEmpName(employee.getEmpName());
//            根据签到的时间判断状态
            if (attendenceDTOTemp.getStatus()==0){
                attendenceDTOTemp.setAtteStatus("迟到");
            } else if (attendenceDTOTemp.getStatus() == 1) {
                attendenceDTOTemp.setAtteStatus("正常");
            } else if (attendenceDTOTemp.getStatus() == 2) {
                attendenceDTOTemp.setAtteStatus("早退");
            }else {
                attendenceDTOTemp.setAtteStatus("未签到");
            }

            list.add(attendenceDTOTemp);
        }
        page.setRecords(list);
        return page;
    }
}
